package foo.bar.wiki.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.UUID;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
@Entity
@Table(name="TBL_COMMENT")
@EntityListeners({EntityAuditListener.class})
@NamedQueries({
    @NamedQuery(
        name="Comment_findCommentById",
        query="SELECT comment FROM Comment AS comment WHERE comment.id = :commentId")        
})
public class Comment implements Identifiable, CreationInfoPersistable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Basic
    @Column(name="UID")
    private String uid = UUID.randomUUID().toString();

    @Basic
    @Column(name="CONTENT")
    private String content;

    @ManyToOne(targetEntity = Page.class)
    @JoinColumn(name = "PAGE_ID", referencedColumnName = "ID")
    private Page page;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @Transient
    private String decoratedContent;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }


    public String getRenderedContent() {
        return decoratedContent;
    }

    public void setRenderedContent(String decoratedContent) {
        this.decoratedContent = decoratedContent;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Comment comment = (Comment) o;

        if (uid != null ? !uid.equals(comment.uid) : comment.uid != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (uid != null ? uid.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (page != null ? page.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (creator != null ? creator.hashCode() : 0);
        return result;
    }
}
